package com.hasidea.android.smswish;

import java.util.ArrayList;
import java.util.List;

import com.hasidea.android.smswish.util.MsgSentManager;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.Contacts.Phones;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleCursorAdapter;

public class MyContacts extends Activity implements OnItemClickListener {
	private Button sms_button, store_button, contact_button;
	private Cursor cursor;
	private ListView contactsList;
	private Button goback_button, aboutus_button;
	 private TextView tip;
	List<ContactBean> contactList = new ArrayList<ContactBean>();

	String msgstr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mycontacts);
		setViews();
		showListView();
		setListeners();
		msgstr = getIntent().getStringExtra(Intent.EXTRA_TEXT);		
		
	}

	private void setViews() {
		tip = (TextView)findViewById(R.id.tip);
		contactsList = (ListView) findViewById(R.id.mycontact_ListView);
		goback_button = (Button) findViewById(R.id.goback_Button);
		aboutus_button = (Button) findViewById(R.id.aboutus_Button);
		sms_button = (Button) findViewById(R.id.sms_button);
		store_button = (Button) findViewById(R.id.store_button);
		contact_button = (Button) findViewById(R.id.contact_button);
	}

	private void setListeners() {
		contactsList.setOnItemClickListener(this);
		goback_button.setOnClickListener(gobackListener);
		aboutus_button.setOnClickListener(aboutusListener);
		sms_button.setOnClickListener(sms_buttonListener);
		store_button.setOnClickListener(store_buttonListener);
		contact_button.setOnClickListener(contact_buttonListener);
		
	}

	private void showListView() {
		cursor = getContentResolver().query(Phones.CONTENT_URI,
				new String[] { "_id", "name", "number" }, null, null,
				"name desc");

	startManagingCursor(cursor);
		ListAdapter adapter = new SimpleCursorAdapter(this,R.layout.row_contactlist, cursor,
				new String[] { android.provider.Contacts.People.Phones.NAME,
						android.provider.Contacts.People.Phones.NUMBER },
				new int[] { R.id.contactName_TextView,
						R.id.contactPhone_TextView });

		contactsList.setAdapter(adapter);

		if(adapter.getCount()>0){
			tip.setVisibility(View.GONE);
		}else{
			tip.setVisibility(View.VISIBLE);
		}
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

	}

	private Button.OnClickListener gobackListener = new Button.OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub

			// Intent intent = new Intent();
			// intent.setClass(MyFavorites.this, MyMsg.class);
			// startActivity(intent);
			finish();
		}
	};
	private Button.OnClickListener aboutusListener = new Button.OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub

			Intent intent = new Intent();
			intent.setClass(MyContacts.this, AboutUs.class);
			startActivity(intent);
			finish();
		}
	};
	
	private Button.OnClickListener sms_buttonListener = new Button.OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub

			Intent intent = new Intent();
			intent.setClass(MyContacts.this, MyMsg.class);
			startActivity(intent);
			finish();
		}
	};
	private Button.OnClickListener contact_buttonListener = new Button.OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub

			Intent intent = new Intent();
			intent.setClass(MyContacts.this, MyContacts.class);
			startActivity(intent);
			finish();

		}
	};
	private Button.OnClickListener store_buttonListener = new Button.OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub

			Intent intent = new Intent();
			intent.setClass(MyContacts.this, MyFavorites.class);
			startActivity(intent);
			finish();
		}
	};
}
